# Maintainer: Alexander Baldeck <alexander@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

pkgname=xorg-xdm
pkgver=1.1.12
pkgrel=3
pkgdesc="X Display Manager"
arch=('x86_64')
url="https://xorg.freedesktop.org/"
license=('custom')
depends=('pam' 'libxaw' 'libxinerama' 'xorg-xrdb' 'xorg-sessreg' 'libxft' 'systemd' 'libxcrypt')
makedepends=('xorg-util-macros' 'xtrans')
backup=(etc/X11/xdm/Xaccess etc/X11/xdm/Xresources etc/X11/xdm/Xservers etc/X11/xdm/xdm-config etc/pam.d/xdm etc/X11/xdm/Xsetup_0 etc/X11/xdm/Xsession)
source=(${url}/releases/individual/app/xdm-${pkgver}.tar.bz2
        Xsession-loginshell.patch
        Xsession-xsm.patch
        xdm-1.0.5-sessreg-utmp-fix-bug177890.patch
        xdm.pam
        libsystemd.patch
        nolisten.patch)
sha512sums=('1a4be0a070ced5db8fda6fc74794c9f9ed0cb37fa440fda6a3a7652aff62dfc3d7ba68b9facf054671ebf0f4db2a0eec29d0aa3716e3407ccd5529bac3553bdb'
            'cf79d5ced77568f346e0955d955438ce27dae99cca21fdd80df779c97cfc03b3b15b561d483f8a8206e1db0785434b816b3474d6cc20c5adb8072c0985254c69'
            '9ba9b515d18c595cec402c7d4f2a8c4b9b10d99ee1bb0a86f2c72c813962a325c3c6e85ea5387f11819f8c046761494d6e54df0cb33bb4ab1e87ef354dc61c5b'
            '698d401899660708dc17eaa84bd2323426afa4c3c094ff8d4a8d9c54e26e073f40dafed67636855bb230f351523a88f1f1ed1ec443d77d92ef65646b5a6976d5'
            'cb912013a294f0801b357a43f3e5313ffa9ac5fcc493b2318843983388eb0b839c84060a97c355e12ca03f3b056644aa4a2bb8a74ed73a0f2405816b8d6efdc0'
            'f30243aa8343d245e32a80953dc990d95d0b9048574a1ae777cb81cde14a0e167e544df1d40d7316ec8465571bd6b68cda984bb40c1fe99fc7ce348247cdca71'
            '36136bdec96dbe034d93b68f14dbd34663e3423520eb8674a5760c7e6379006b32e42c43691cf6d91f9d683bee07e117c7ffc326c50b9e24f7515495e12cd89b')

prepare() {
  cd xdm-${pkgver}
  patch -Np0 -i "${srcdir}/Xsession-loginshell.patch"
  patch -Np1 -i "${srcdir}/Xsession-xsm.patch"
  patch -Np0 -i "${srcdir}/xdm-1.0.5-sessreg-utmp-fix-bug177890.patch"
  patch -Np1 -i ../nolisten.patch
  patch -Np1 -i ../libsystemd.patch
  autoreconf -vfi
}

build() {
  cd xdm-${pkgver}

  # FS#63867 XDM's default userPath / systemPath hide /usr/local 
  unset DEF_USER_PATH

  ./configure --prefix=/usr \
      --disable-xdm-auth \
      --disable-static \
      --with-xdmconfigdir=/etc/X11/xdm \
      --with-xdmscriptdir=/etc/X11/xdm \
      --with-pixmapdir=/usr/share/xdm/pixmaps \
      DEF_USER_PATH="/usr/local/bin:/usr/bin:/bin" \
      DEF_SYSTEM_PATH="/usr/local/bin:/usr/bin:/bin"

  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make
}

check() {
  cd xdm-${pkgver}
  make check
}

package() {
  cd xdm-${pkgver}
  make DESTDIR="${pkgdir}" install
  install -m755 -d "${pkgdir}/var/lib/xdm"
  install -m755 -d "${pkgdir}/etc/pam.d"
  install -m644 "${srcdir}/xdm.pam" "${pkgdir}/etc/pam.d/xdm"
  install -m755 -d "${pkgdir}/usr/share/licenses/${pkgname}"
  install -m644 COPYING "${pkgdir}/usr/share/licenses/${pkgname}/"

  sed -i -e 's/\/X11R6//g' "${pkgdir}"/etc/X11/xdm/*

  sed -i 's|^Alias=.*|Alias=display-manager.service|' \
    "$pkgdir/usr/lib/systemd/system/xdm.service"
}
